// pagesCollect/pages/collect/edit/components/location/location.js
const app = getApp()

Component({
  properties: {
    position: {
      type: Object,
      value: {},
    },
    title: {
      type: String,
      value: '定位',
    },
    addressShow: {
      type: Boolean,
      value: false
    },
    address: {
      type: String,
      value: '',
    },
    icon: {
      type: String,
      value: '/image/icon/icon_dw.png',
    },
  },
  lifetimes: {
    attached() {
      //初始化时间选择器
      this.getlocation()
    },
    detached() { }
  },
  //对 <2.2.3 版本基础库的兼容
  attached() {
    //初始化时间选择器
    this.getlocation()
  },
  data: {
    location: {
      longitude: null,
      latitude: null
    },
    scale: 16,
    marker: []
  },
  observers: {
    'position': function (value) {
      let {
        longitude,
        latitude
      } = value;
      if (longitude && latitude) {
        this.setNormal(longitude, latitude)
      }
    }
  },
  methods: {
    getlocal() {
      let that = this;
      wx.getLocation({
        type: 'gcj02',
        success(res) {
          that.setData({
            location: {
              longitude: res.longitude,
              latitude: res.latitude
            }
          })
          that.setMarker(res.longitude, res.latitude)
          if (that.data.addressShow) {
            that.getAddressStr(res.longitude, res.latitude)
          }
        }
      })
    },
    mapClick(e) {
      let {
        longitude,
        latitude
      } = e.detail;
      this.setMarker(longitude, latitude)
      this.triggerEvent('localChg', {
        longitude, latitude
      })
      if (this.data.addressShow) {
        this.getAddressStr(longitude, latitude)
      }

    },
    getlocation() {
      let {
        longitude,
        latitude
      } = this.data.position;
      if (longitude && latitude) {
        this.setNormal(longitude, latitude)
      } else {
        let that = this;
        wx.getLocation({
          type: 'gcj02',
          success(res) {
            let {
              longitude,
              latitude
            } = that.data.position;
            if (longitude && latitude) {
              that.setNormal(longitude, latitude)
            } else {
              that.setData({
                location: {
                  longitude: res.longitude,
                  latitude: res.latitude
                }
              })
            }

          }
        })
      }
    },
    setNormal(longitude, latitude) {
      this.setMarker(longitude, latitude);
      this.setData({
        location: {
          longitude: longitude,
          latitude: latitude
        }
      })
    },
    setMarker(longitude, latitude) {
      this.setData({
        marker: [{
          id: 1,
          longitude: longitude,
          latitude: latitude,
          width: 30,
          height: 30,
          iconPath: '../../../../../../image/icon/icon_dw_marker.png',
        }]
      })
    },
    addressChg(e) {
      this.triggerEvent('addressChg', e.detail.value)
    },
    getAddressStr(longitude, latitude) {
      let parameters = `key=ce0b625eb2aa9108884f7b9ec86fe227&location=${longitude},${latitude}`
      wx.request({
        url: 'https://restapi.amap.com/v3/geocode/regeo?'+parameters,
        method: 'get',
        header: {},
        timeout: 100000,
        success: (res) => {
          this.triggerEvent('addressChg', res.data.regeocode.formatted_address)
        }
      })
    
    }
  }
})